package com.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.fileupload.FileUpload;
import org.primefaces.event.RowEditEvent;

import com.controllers.EmployeeController;
import com.controllers.GroupController;
import com.model.Employee;
import com.model.Group;

@Named
@ViewScoped
public class TableBean implements Serializable {

	private static final long serialVersionUID = -6361733300885842590L;

	private EmployeeController employeeController;

	private List<Employee> employees = new ArrayList<Employee>();

	private Employee selectedEmployee = new Employee();

	private List<Group> groups;

	private Group selectedGroup = new Group();

	private GroupController groupController;

	private FileUpload imageName;

	@Inject
	public TableBean(EmployeeController employeeController,
			GroupController groupController, DisplayHelperBean displayHelperBean) {
		this.employeeController = employeeController;
		this.groupController = groupController;
	}

	@PostConstruct
	public void initializeAccesses() {
		employees = employeeController.getEmployees();
		groups = groupController.getGroups();
	}

	public void save(ActionEvent actionEvent) {
		selectedEmployee.setGroup(selectedGroup);
		selectedEmployee.setImage("images/personal/lteves.jpg");
		employeeController.save(selectedEmployee);
		employees = employeeController.getEmployees();
		TasksTableBean.employees = employees;
	}

	public void delete() {
		employeeController.delete(selectedEmployee);
		employees.remove(selectedEmployee);
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(selectedEmployee.getFirstname() + " "
						+ selectedEmployee.getLastname() + " ha sido borrado"));
	}

	public void saveRow(RowEditEvent rowEditEvent) {
		Employee employee = (Employee) rowEditEvent.getObject();
		employeeController.save(employee);
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(employee.getFirstname() + " "
						+ employee.getLastname() + " ha sido actualizado"));
	}

	public void newEmployee(ActionEvent actionEvent) {
		selectedEmployee = new Employee();
	}

	public void editEmployee(ActionEvent actionEvent) {
		selectedGroup = selectedEmployee.getGroup();
		selectedEmployee.setPassword("");
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public Employee getSelectedEmployee() {
		return selectedEmployee;
	}

	public void setSelectedEmployee(Employee selectedEmployee) {
		this.selectedEmployee = selectedEmployee;
	}

	public Group getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(Group selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public FileUpload getImageName() {
		return imageName;
	}

	public void setImageName(FileUpload imageName) {
		this.imageName = imageName;
	}

}